package com.agileboot.domain.banner.db;

import cn.hutool.core.collection.CollUtil;
import com.agileboot.common.core.page.PageDTO;
import com.agileboot.common.exception.ApiException;
import com.agileboot.common.exception.error.ErrorCode;
import com.agileboot.domain.banner.command.HomeBannerCommand;
import com.agileboot.domain.banner.query.HomeBannerQuery;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Objects;

/**
 * 首页banner表(HomeBanner)表服务实现类
 *
 * @author lx
 * @since 2023-12-08
 */
@Service
public class HomeBannerServiceImpl extends ServiceImpl<HomeBannerMapper, HomeBanner> implements HomeBannerService {

    @Autowired
    private HomeBannerMapper homeBannerMapper;

    /**
     * 列表
     * @param homeBannerQuery
     * @return
     */
    @Override
    public PageDTO<HomeBanner> getHomeBannerList(HomeBannerQuery homeBannerQuery) {
        //校验参数
        Assert.isTrue(Objects.nonNull(homeBannerQuery),"参数为空");
        Page<HomeBanner> homeBannerPage = homeBannerQuery.toPage();
        LambdaQueryWrapper<HomeBanner> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(homeBannerQuery.getName()),HomeBanner::getName,homeBannerQuery.getName());
        queryWrapper.eq(homeBannerQuery.getStatus()!=null,HomeBanner::getStatus,homeBannerQuery.getStatus());

        Page<HomeBanner> page = page(homeBannerPage, queryWrapper);

        return new PageDTO<HomeBanner>(page.getRecords(),page.getTotal());
    }

    /**
     * 修改
     * @param homeBannerCommand
     */
    @Override
    public void editHomeBanner(HomeBannerCommand homeBannerCommand) {
        //校验参数
        Assert.isTrue(Objects.nonNull(homeBannerCommand),"参数为空");
        Assert.isTrue(StringUtils.isNotBlank(homeBannerCommand.getId().toString()),"请求参数为空");
        HomeBanner homeBanner = new HomeBanner();
        BeanUtils.copyProperties(homeBannerCommand,homeBanner);
        baseMapper.updateById(homeBanner);
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    public void delHomeBanner(List<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            throw new ApiException(ErrorCode.Business.COMMON_BULK_DELETE_IDS_IS_INVALID);
        }
        HashSet<Long> idList = new HashSet<>(ids);
        homeBannerMapper.deleteBatchIds(idList);
    }

    @Override
    public void saveHomeBanner(HomeBannerCommand homeBannerCommand) {
        Assert.isTrue(StringUtils.isNotBlank(homeBannerCommand.getImgUrl()),"参数为空");
        Assert.isTrue(StringUtils.isNotBlank(homeBannerCommand.getName()),"参数为空");
        HomeBanner homeBanner = new HomeBanner();
        BeanUtils.copyProperties(homeBannerCommand,homeBanner);
        homeBanner.setStatus(1);
        this.save(homeBanner);
    }

    @Override
    public PageDTO<HomeBanner> getHomeBannerPage() {
        Page<HomeBanner> bannerPage = new Page<>(1, 5);
        LambdaQueryWrapper<HomeBanner> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HomeBanner::getStatus,1);
        queryWrapper.eq(HomeBanner::getDeleted,0);
        Page<HomeBanner> page = page(bannerPage, queryWrapper);
        return new PageDTO<HomeBanner>(page.getRecords(),page.getTotal());
    }
}

